package solutions.lib;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

public class BaseSolution {
    final static StringBuilder builder = new StringBuilder();

    final static Gson gson = new Gson();

    protected static <T> T parseString(String origin, TypeToken<T> typeToken) {
        return gson.fromJson(origin, typeToken.getType());
    }

    protected static void printVals(int[] val) {
        for (int v : val) {
            builder.append(v).append(", ");
        }
        end();
    }

    @SafeVarargs
    protected static <T> void printVals(T... val) {
        for (T v : val) {
            builder.append(v).append(", ");
        }
        end();
    }

    protected static void printVals(int[][] val) {
        for (int[] v : val) {
            builder.append('[');
            for (int i = 0; i < v.length; i++) {
                builder.append(v[i]);
                if (i != v.length - 1) {
                    builder.append(" ,");
                }
            }
            builder.append(']').append('\n');
        }
        end();
    }

    protected static <V> void printVals(List<V> list) {
        for (V v : list) {
            builder.append(v.toString()).append("\n");
        }
        end();
    }

    private static void end() {
        System.out.println(builder);
        builder.delete(0, builder.length());
    }
}
